﻿using System;
using System.Timers;

using SharpHook;
using SharpHook.Native;

using WindowsInput;

Console.WriteLine($"-----------程序已启动-----------");
var random = new Random();
var simulator = new EventSimulator();
var hook = new TaskPoolGlobalHook();
var state = false;

hook.KeyPressed += (sender, e) => OnKeyPressed(sender, e);
hook.KeyReleased += (sender, e) => OnKeyReleased(sender, e);
await hook.RunAsync();

void OnKeyPressed(object? sender, KeyboardHookEventArgs e)
{
    if (e.Data.KeyCode == KeyCode.VcSpace)
    {
        state = false;
        return;
    }
}

void OnKeyReleased(object? sender, KeyboardHookEventArgs e)
{
    if (state) return;
    if (e.Data.KeyCode == KeyCode.VcSpace && !state)
    {
        state = true;
        Console.WriteLine($"\r\n【{DateTime.Now}】-----------开始扫地-----------");
        for (int i = 0; i < 10; i++)
        {
            simulator.SimulateKeyPress(KeyCode.VcSpace);
            // 生成随机的延迟时间（xx到xx毫秒之间）
            int delay = random.Next(8, 55);
            Thread.Sleep(delay);
            Console.WriteLine($"扫地中。。。次数：{i + 1},延迟：{delay}ms");
        }
    }
}
